﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Bidder_Companyaffirm : System.Web.UI.Page
{
    private const int SpPrice = 10;



    private const int ExpertPrice1 = 16;
    private const int Accept_Company0 = 17;
    private const int Accept_Company = 18;


    public string _strNone = "block";
    string _strProjectId = "";
    string _strUserType = "";
    string _project = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        _project = SysFun.IsSQL(Request.QueryString["projectid"]);
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();

        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {

            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];

            Label1.Text = "";
            //string sql = "select top 1 submit,YiJiaSwitch from dbo.Affirm";
            //Db che = new Db();
            //DataTable tb = new DataTable();
            //tb = che.DataTable(sql);
            //if (tb.Rows.Count > 0)
            //{
            //    YiJiaSwitch = int.Parse(tb.Rows[0].ItemArray[1].ToString());
            //    if (tb.Rows[0].ItemArray[0].ToString() == "0")
            //    {
            //        Response.Write("<script>window.alert('限价开关没开！');window.close();</script>");

            //        return;
            //    }
            //}
            //tb.Dispose();
            //che.Close();
            _strProjectId = _project;
            //查询
            showGridView();


            if (_Account != null)
            {
                _strUserType = SysFun.ToTrim(_Account.Type);

                if (Request.Form["PrintPrice"] == "打印")
                {
                    PrintCompanyPrice();
                }

            }
        }

    }

    public void showGridView()
    {

        string _strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "Affirm";                                               //要查询的表明
        dbPage.Key = "goodsid";                                               //表的主键    
        dbPage.Where = _strConditions;                                           //查询条件    
        dbPage.SelectField = " GoodsId, ProductName, Medicinemodel, Outlookc, unit,Factor, cengci, CompanyName_sc, "
            + "CompanyName_TB, UserCode_TB,   bidprice,Accept_Company,submit,'' as bj2";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页                 
        if (Request.Form["PrintPrice"] == "打印")
        {
            dbPage.PageSize = 1;                                                    //没有显示记录条数   
        }
        else
        {
            dbPage.PageSize = 10;                                                    //没有显示记录条数   
        }
        dbPage.OrderBy = "GoodsId";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView2.DataSource = dr;
        GridView2.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键 

    }
    /// <summary>
    /// 拼接查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string _strWhere;
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {
            _strProjectId = _project;

            _strWhere = " UserCode_TB= '" + _Account.AccountName + "' And";

            //药品通用名
            string _strProductName = SysFun.ToTrim(txtProductName.Text);
            if (_strProductName != "")
            {
                _strWhere += " ProductName like '%" + _strProductName + "%' And";
            }

            //药品流水号
            string _strGoodsid = txtGoodsId.Text.Trim();
            if (_strGoodsid != "")
            {
                _strWhere += " Goodsid ='" + _strGoodsid + "' And";
            }

            //生产企业名称
            string _strCompanyNameSC = txtCompanyNameSC.Text.Trim();
            if (_strCompanyNameSC != "")
            {
                _strWhere += " CompanyName_sc like '%" + _strCompanyNameSC + "%' And";
            }
            //限价限价报价
            string _strAcceptCompany = lstAcceptCompany.SelectedValue;
            switch (_strAcceptCompany)//第一次的
            {
                case "全部":
                    break;
                case "已接受":
                    //未接受已报价
                    _strWhere += " Accept_Company=1 And";
                    break;
                case "未接受":
                    _strWhere += " (accept_company=0 or accept_company is null) And";
                    break;
            }

            if (_strWhere != "")
            {
                _strWhere = "  " + _strWhere.Substring(0, _strWhere.Length - 3);
            }
        }
        else
        {
            _strWhere = "";

        }
        return _strWhere;
    }




    

    protected void PrintCompanyPrice()
    {

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        // 添加头信息，为"文件下载/另存为"对话框指定默认文件名 
        // Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(accountid.ToString()) + ".xls");
        Response.AddHeader("Content-Disposition", "attachment; filename=11.xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentEncoding = System.Text.Encoding.UTF7;  //解决将gridview导出成excel时出现乱码  
        Response.ContentType = "application/ms-excel";
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.GridView2.RenderControl(oHtmlTextWriter);
        System.Text.StringBuilder strHtml = new System.Text.StringBuilder();
        strHtml.Append(oStringWriter.ToString().Substring(0, 197));
        strHtml.Append("<tr border=\"0\"><td colspan='15'align=\"center\"><strong><span style=\"font-size: 24pt\">限价品种投标人确认表</span></strong></td></tr>");
        strHtml.Append(oStringWriter.ToString().Substring(198, oStringWriter.ToString().Length - 215));
        strHtml.Append("<tr border=\"0\"><td colspan='3' align='right'><strong>投标人签字：</strong></td><td colspan='4'  style='width: 18px'></td><td  colspan='2' align='right'><strong>时间：</strong></td><td  colspan='6'align='left' >" + DateTime.Now.ToString() + "</td></tr>");

        strHtml.Append(oStringWriter.ToString().Substring(oStringWriter.ToString().Length - 16, 16));
        // 把文件流发送到客户端 
        Response.Write(strHtml.ToString());
        // 停止页面的执行
        Response.End();

    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //OverRide　为了使导出成Excel可行！
    }



    protected void Button3_Click(object sender, EventArgs e) //提交意见
    {
        string strSql = "", strBidY = "", strBidN = "";
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        foreach (string strId in SysFun.ToTrim(Request.Form["GoodsId"]).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries))
        {
            if (SysFun.ToInt(Request.Form["radBid" + strId]) == 1)
                strBidY = strBidY + strId + ",";
            else
                strBidN = strBidN + strId + ",";
        }
        Db db = new Db();
        if (strBidN != "")
        {

            strBidN = strBidN.Substring(0, strBidN.Length - 1);
            //strSql = "update Affirm set status=5 where goodsid in  (" + strBidN + ") "; // 不同意 status=5 进入重点监控
            //db.Command(strSql);
            strSql = "update Affirm set Accept_Company = 0 where goodsid in  (" + strBidN + ") "; // 不同意 status=5 进入重点监控
            db.Command(strSql);
            db.Close();
            HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "修改", "Bargaining", strSql, "../Expert/companyyijia.aspx");
        }
        if (strBidY != "")
        {
            strBidY = strBidY.Substring(0, strBidY.Length - 1);
            //strSql = "update Affirm set isbid=1 where goodsid in  (" + strBidY + ") "; // 同意 中标
            //db.Command(strSql);
            strSql = "update Affirm set Accept_Company = 1 where goodsid in  (" + strBidY + ") ";
            db.Command(strSql);
            db.Close();
            HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "修改", "Bargaining", strSql, "../Expert/companyyijia.aspx");
        }
        db.Close();

        //查询
        showGridView();
        //绑定字段

        ////判断提交状态,来觉得提交按钮是否显示

    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string _strAccept_Company = e.Row.Cells[13].Text;
            string _strSubmit = e.Row.Cells[14].Text;

            switch (_strAccept_Company)
            {
                case "1":
                    if (_strSubmit == "1")
                    {
                        e.Row.Cells[12].Text = "<input type=radio name ='radBid" + e.Row.Cells[0].Text + "' value='1' checked>是<input type=radio name ='radBid" + e.Row.Cells[0].Text + "' value='0'>否";
                        Button3.Visible = true;
                    }
                    else
                    {
                        e.Row.Cells[12].Text = "是";
                        Button3.Visible = false;
                    }

                    break;
                 default :
                    if (_strSubmit == "1")
                    {
                        e.Row.Cells[12].Text = "<input type='radio' name ='radBid" + e.Row.Cells[0].Text + "' value='1'>是<input type=radio name ='radBid" + e.Row.Cells[0].Text + "' value='0' checked>否";
                        Button3.Visible = true;
                    }
                    else
                    {
                        e.Row.Cells[12].Text = "否";
                        Button3.Visible = false;
                    }
                    break;


            }
            e.Row.Cells[2].Text = e.Row.Cells[2].Text + "<input  type='hidden' name='GoodsId' value='" + e.Row.Cells[0].Text + "'>";
        }
        
        e.Row.Cells[13].Visible = false;
        e.Row.Cells[14].Visible = false;
    }
}
